<div id="container" class="span9 paddingnone fixContainerWidth" style="margin-top: 0px;">
    <form id="frmUSSD">
        <div class="form-body">
            <div class="form-body margin-top-20" id="USSD_send_div" data-bind="visible: USSDLocation()==0">
                <div class="form-title" data-trans="USSD"></div>
                <div class="content">
                    <div class="row form-group">
                        <label class="span4 side-right" data-trans="ussd_send_to"></label>
                        <div class="span8">
                            <input type="text" size="20" id="USSD_send" class="required form-control" data-bind="value:USSDSend,valueUpdate:'afterkeydown'">
                        </div>
                    </div>
                    <div class="form-buttons">
                        <input type="button" data-trans='ussd_send' data-bind="disable:USSDSend().length<=0,css:{disabled:USSDSend().length<=0},click:sendToNet" id="sendToNet" class="btn-1 marginright10" />
                        <input type="reset" data-trans='ussd_cancel' data-bind="disable:USSDSend().length<=0,css:{disabled:USSDSend().length<=0},click:resetUSSD" id="cancelToNet" class="btn-1 marginright10" />
                    </div>
                </div>
            </div>
            <div class="form-body margin-top-20" id="USSD_reply_div" data-bind="visible: USSDLocation()==1">
                <div class="form-title" data-trans='ussd_contents'></div>
                <div class="content">
                    <div class="row form-group">
                        <label class="span4 side-right" data-trans="ussd_contents"></label>
                        <div class="span8">
                            <textarea name="USSD_Content" id="USSD_Content" rows="8" cols="50" readonly="readonly"></textarea>
                        </div>
                    </div>
                    <div class="form-buttons">
                        <input type="button" data-trans='ussd_cancel' class="btn-1 marginright10" data-bind="click:noReplyCancel" />
                    </div>
                    <div id="ussd_action" data-bind="visible: ussd_action()!=0">
                        <div class="row form-group">
                            <label class="span4 side-right" data-trans="ussd_reply_to"></label>
                            <div class="span8">
                                <input type="text" name="USSD_reply" id="USSD_reply" class="required form-control" size="20" data-bind="value:USSDReply">
                            </div>
                        </div>
                        <div class="form-buttons">
                            <input type="button" data-trans='ussd_reply' data-bind="click:replyToNet" class="btn-1 marginright10" />
                        </div>
                    </div>
                </div>
            </div>
            <div class="form-note" style="display: none;">
                <div class="notes-title">&nbsp;</div>
                <ul class="notes-content">
                    <li data-trans="ussd_note1"></li>
                </ul>
            </div>
        </div>
    </form>
</div>

<script>
    $(function() {
        $("#list-nav").find(".active").removeClass("active")
        $("#list-nav").find("[mid='ussd']").addClass("active");

        var service = window._service;
        var config = window._service.config;

        var initUSSD = true;
        var timeOutFlag = 0; //计时
        var reply_flag = false; //是否已经得到回复
        var USSDLocation = {
            SEND: 0,
            REPLY: 1
        };
        var callbackTemp;
        var ussd_action = 1; //初始化为成功获得返回消息
        var interval = 0;

        /**
         * @class USSDInformationViewModel
         * @constructor
         */
        function USSDInformationViewModel() {
            var self = this;

            self.hasUpdateCheck = config.HAS_UPDATE_CHECK;
            self.ussd_action = ko.observable(ussd_action);
            self.USSDLocation = ko.observable(USSDLocation.SEND);
            self.USSDReply = ko.observable("");
            self.USSDSend = ko.observable("");
            self.hasDdns = config.DDNS_SUPPORT;
            self.hasProbe = config.HAS_PROBE;

            /**
             * 发送USSD命令
             * @method sendToNet
             */
            self.sendToNet = function() {
                timeOutFlag = 0;
                window.clearInterval(interval);
                var command = self.USSDSend();
                if (('string' != typeof(command)) || ('' == command)) {
                    showAlert("ussd_error_input");
                    return;
                }

                showLoading('waiting');

                var params = {};
                params.operator = "ussd_send";
                params.strUSSDCommand = command;
                params.sendOrReply = "send";

                service.getUSSDResponse(params, function(result, content) {
                    hideLoading();
                    if (result) {
                        resetUSSD();
                        self.USSDLocation(USSDLocation.REPLY);
                        self.ussd_action(content.ussd_action);
                        $("#USSD_Content").val(decodeMessage(content.data, true));
                        reply_flag = false;
                        timeOutFlag = 0;
                    } else {
                        showAlert(content);
                    }
                });
            };

            /**
             * 回复USSD消息
             * @method replyToNet
             */
            self.replyToNet = function() {
                timeOutFlag = 0;
                window.clearInterval(interval);
                var command = self.USSDReply();
                if (('string' != typeof(command)) || ('' == command)) {
                    showAlert("ussd_error_input");
                    return;
                }

                showLoading('waiting');

                var params = {};
                params.operator = "ussd_reply";
                params.strUSSDCommand = command;
                params.sendOrReply = "reply";

                service.getUSSDResponse(params, function(result, content) {
                    hideLoading();
                    if (result) {
                        self.ussd_action(content.ussd_action);
                        $("#USSD_Content").val(decodeMessage(content.data, true));
                        reply_flag = false;
                        resetUSSD();
                        timeOutFlag = 0;
                    } else {
                        showAlert(content);
                    }
                });
            };

            /**
             * 取消回复USSD消息
             * @method noReplyCancel
             */
            self.noReplyCancel = function() {
                timeOutFlag = 0;
                reply_flag = true;
                window.clearInterval(interval);
                // 判断是否需要下发取消命令
                if (parseInt(self.ussd_action()) != 1) {
                    resetUSSD();
                    self.USSDLocation(USSDLocation.SEND);
                    return;
                }

                service.USSDReplyCancel(function(result) {
                    if (result) {
                        resetUSSD();
                        self.USSDLocation(USSDLocation.SEND);
                    } else {
                        showAlert("ussd_fail");
                    }
                });
            };

            /**
             * 判断是否响应超时
             * @method timeOutVerify
             */
            function timeOutVerify() {
                if (!reply_flag) {
                    if (timeOutFlag < 29) {
                        timeOutFlag++;
                    } else {
                        reply_flag = true;
                        window.clearInterval(interval);
                        showAlert("ussd_operation_timeout");
                        self.USSDReply("");
                        self.USSDSend("");
                        self.USSDLocation(USSDLocation.SEND);
                        timeOutFlag = 0;
                    }
                } else {
                    reply_flag = true;
                    window.clearInterval(interval);
                    timeOutFlag = 0;
                }
            };

            cancelUSSD = function() {
                service.USSDReplyCancel(function(result) {});
            };

            /**
             * 重置USSD输入命令
             * @method resetUSSD
             */
            resetUSSD = function() {
                self.USSDReply("");
                self.USSDSend("");
            };

            //如果首次进入USSD菜单，先发送USSD取消命令，进行初始化
            if (initUSSD && !window._isInitedUSSD) {
                cancelUSSD();
                initUSSD = false;
            }
        }

        /**
         * 初始化
         * @method init
         */
        function init() {
            var container = $('#container')[0];
            ko.cleanNode(container);
            var vm = new USSDInformationViewModel();
            ko.applyBindings(vm, container);

        }

        init();
    });
</script>